﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicModuleConfiguration.Module
{
    //配置提供者抽象类 读取配置文件 ttt.txt 解析内容 内容交给IConfiguration
    internal class TextConfigurationProvider : ConfigurationProvider
    {
        public TextConfigurationSource textConfigurationSource;

        public TextConfigurationProvider(TextConfigurationSource textConfiguration)
        {
            textConfigurationSource = textConfiguration;
        }

        public override void Load()
        {
            var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            if (!File.Exists(textConfigurationSource.filePath))
            {
                throw new FileNotFoundException("文件不存在");
            }
            var lines = File.ReadAllLines(textConfigurationSource.filePath);
            foreach (var line in lines)
            {
                var keyValue = line.Split('=');
                if (keyValue.Length == 2)
                    data.Add(keyValue[0].Trim(), keyValue[1].Trim());
            }
            Data = data;
        }
    }
}
